package com.right.health.app.ui.medical;

import android.os.Bundle;
import android.view.View;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.AllergySourceResponse;
import com.right.health.app.net.SickHistoryApi;
import com.right.health.app.ui.medical.adapter.AllergyListAdapter;
import com.right.health.app.widget.ListViewEx;
import com.right.health.app.widget.ProgressLayout;

import java.util.List;

import static com.right.health.app.constants.BroadCastCode.REFRESH_COMMON;

/*
 * Created by hopeliao on 2019-09-30
 * 过敏史
 *
 */
public class AllergyListActivity extends BaseActivity implements OnHttpListener, View.OnClickListener {

    private static final int HTTP_CODE_UPDATE = 1;

    private AllergyListAdapter mAdapter;

    private SickHistoryApi mSickHistoryApi;

    private ProgressLayout mProgressLayout;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.allergy_list);
    }

    @Override
    public void findView() {
        ListViewEx listViewEx = (ListViewEx) findViewById(R.id.listview);
        mProgressLayout = findViewById(R.id.pl);
        mAdapter = new AllergyListAdapter(this);
        listViewEx.setAdapter(mAdapter);

        findViewById(R.id.btn_save).setOnClickListener(this);
        findViewById(R.id.ib_add).setOnClickListener(this);

    }

    @Override
    public void initialize() {
        mSickHistoryApi = new SickHistoryApi();
        mSickHistoryApi.setListener(this);

        onRefresh();
    }

    private void onRefresh() {
        mSickHistoryApi.getAlleraySouce(App.getInstance().getCurrentMember().id);
    }

    @Override
    public void onReceiveBroadcast(int intent, Bundle bundle) {
        super.onReceiveBroadcast(intent, bundle);
        if (intent == REFRESH_COMMON) {
            onRefresh();
        }
    }

    private void send2Net() {
        List<AllergySourceResponse.AllergySourceInfo> dataSource = mAdapter.getDataSource();
        if (!CollectionUtil.isEmpty(dataSource)) {
            mSickHistoryApi.addRequestCode(HTTP_CODE_UPDATE);
            mSickHistoryApi.updateAlleraySouce(App.getInstance().getCurrentMember().id, dataSource);
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
            mProgressLayout.showInfo("获取失败");
        } else {
            if (request.requestCode == HTTP_CODE_UPDATE) {
                ToastUtil.show(this, "修改成功");
            } else {
                if (response.targetData instanceof AllergySourceResponse) {
                    AllergySourceResponse allergySourceResponse = response.cast(AllergySourceResponse.class);
                    mAdapter.setDataSource(allergySourceResponse.anaphylactogens);
                    if (!CollectionUtil.isEmpty(allergySourceResponse.anaphylactogens)){
                        mProgressLayout.showContent(findViewById(R.id.scroll_view));
                    }else {
                        mProgressLayout.showInfo(findViewById(R.id.scroll_view),"暂无手术史");
                    }
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                send2Net();
                break;
            case R.id.ib_add:
                startIntent(AllergyAddActivity.class);
                break;
        }
    }
}
